﻿using System.Collections.Generic;
using KMF.Common;

namespace KMF.DAL
{
    public partial class SubCategoryTemplateDb
    {
        public static int Add(SubCategoryTemplateDto model)
        {
            var dao = SharedSqlDao;
            var commnad = dao.GetDbSprocCommand("WSPX_Admin_SubCategoryTemplate_Add");
            try
            {
                commnad.Parameters.Add(dao.CreateParameter("@ProductCateTempMappingId", model.ProductCateTempMappingId));
                commnad.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                commnad.Parameters.Add(dao.CreateParameter("@TempContent", model.TempContent));
                commnad.Parameters.Add(dao.CreateParameter("@DisplayOrder", model.DisplayOrder));
                return (int) commnad.ExecuteScalar();
            }
            finally
            {
                commnad.Connection.Close();
            }
        }

        public static int Update(SubCategoryTemplateDto model)
        {
            var dao = SharedSqlDao;
            var commnad = dao.GetDbSprocCommand("WSPX_Admin_SubCategoryTemplate_Update");
            try
            {
                commnad.Parameters.Add(dao.CreateParameter("@Id", model.Id));
                commnad.Parameters.Add(dao.CreateParameter("@ProductCateTempMappingId", model.ProductCateTempMappingId));
                commnad.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                commnad.Parameters.Add(dao.CreateParameter("@TempContent", model.TempContent));
                commnad.Parameters.Add(dao.CreateParameter("@DisplayOrder", model.DisplayOrder));
                return commnad.ExecuteNonQuery();
            }
            finally
            {
                commnad.Connection.Close();
            }
        }

        public static SubCategoryTemplateDto GetById(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_SubCategoryTemplate_GetById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleDTO<SubCategoryTemplateDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static ViewSubCategoryTempJoinDto GetByIdJoin(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_SubCategoryTemplate_GetByIdJoin");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleDTO<ViewSubCategoryTempJoinDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<ViewSubCategoryTempJoinDto> GetAllByMappingId(int productCateTempMappingId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_SubCategoryTemplate_GetAllByMappingId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductCateTempMappingId", productCateTempMappingId));
                return dao.GetDTOList<ViewSubCategoryTempJoinDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static int Delete(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_SubCategoryTemplate_Delete");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
